require('page/common/header/index.js')

var _order_service = require('service/order-service.js');
const _account_service = require('../../service/account-service');
const { data } = require('browserslist');
var orderForm = {
    init: function () {
        this.bindEvents();
        this.loadOrderForm();
        return this;
    },

    formData: {},

    bindEvents: function () {
        var that = this;
        $('#orderFormSubmit').on('click', function (e) {
            
            e.preventDefault(); // 阻止默认跳转
            that.submit();
            window.location.href = './order-confirmOrder.html'
        });

        // 其他事件绑定（如地址切换）保持不变
        $('#billingLi').on('click', function () {
            $('#billing').show();
            $('#shipping').hide();
            $('#billingLi').addClass('on');
            $('#shippingLi').removeClass('on');
        });
        $('#shippingLi').on('click', function () {
            $('#shipping').show();
            $('#billing').hide();
            $('#shippingLi').addClass('on');
            $('#billingLi').removeClass('on');
        });
    },

    loadOrderForm: function () {

        // 静态信用卡类型（假设后端不再提供此接口）
        const cardTypes = ['Visa', 'MasterCard', 'American Express'];
        let com = document.getElementById("cardType");
        cardTypes.forEach((type, index) => {
            com.options.add(new Option(type, index));
        });

        var that = this;
        _account_service.getCurrentUserInfo(
            function (resp) {
                if (resp.status === 0) {
                    // 填充表单字段（UI展示）
                    $("#creditcard").attr("value", resp.data.creditcard);
                    $("#exprdate").attr("value", resp.data.exprdate);
                    $("#billtofirstname").attr("value", resp.data.firstname);
                    $("#billtolastname").attr("value", resp.data.lastname);
                    $("#billaddr1").attr("value", resp.data.address1);
                    $("#billaddr2").attr("value", resp.data.address2);
                    $("#billcity").attr("value", resp.data.city);
                    $("#billstate").attr("value", resp.data.state);
                    $("#billzip").attr("value", resp.data.zip);
                    $("#billcountry").attr("value", resp.data.country);
    
                    // ✅ 将数据保存到组件的 billToData 属性
                    that.billToData = {
                        firstName: resp.data.firstname,
                        lastName: resp.data.lastname,
                        address1: resp.data.address1,
                        address2: resp.data.address2,
                        city: resp.data.city,
                        state: resp.data.state,
                        zip: resp.data.zip,
                        country: resp.data.country
                    };
                }
            },
            function (errMsg) {
                console.log("获取用户信息失败:", errMsg);
            }
        );

    },

    submit: function () {
        // 收集表单数据
        this.formData = {
            cardType: $('#cardType option:selected').text(),
            creditcard: $('#creditcard').val(),
            exprdate: $('#exprdate').val(),
            
            billTo: {
                firstName: $('#billtofirstname').val(),
                lastName: $('#billtolastname').val(),
                address1: $('#billaddr1').val(),
                address2: $('#billaddr2').val(),
                city: $('#billcity').val(),
                state: $('#billstate').val(),
                zip: $('#billzip').val(),
                country: $('#billcountry').val()
            },
            
            shipTo: {
                firstName: $('#shiptofirstname').val(),
                lastName: $('#shiptolastname').val(),
                address1: $('#shipaddr1').val(),
                address2: $('#shipaddr2').val(),
                city: $('#shipcity').val(),
                state: $('#shipstate').val(),
                zip: $('#shipzip').val(),
                country: $('#shipcountry').val()
            }
        };

        // 将订单数据存储到 localStorage
        localStorage.setItem('pendingOrder', JSON.stringify(this.formData));
        
        // 跳转到确认页面
        window.location.href = './order-confirmOrder.html';
    }
}

module.exports = orderForm.init();